package edu.ncsu.csc.ip.client.run;

import java.io.IOException;

import edu.ncsu.csc.ip.client.PeerClient;
import edu.ncsu.csc.ip.client.protocol.ProtocolClientImpl;
import edu.ncsu.csc.ip.common.Log;

public class RunTask1 {

	public static void main(String[] args) throws IOException {

		int[] rfcNumbers = { 6958, 6959, 6960, 6961, 6962, 6963, 6964, 6965,
				6967, 6968, 6969, 6970, 6971, 6972, 6973, 6974, 6975, 6976,
				6977, 6978, 6979, 6980, 6981, 6982, 6983, 6984, 6985, 6986,
				6987, 6989, 6990, 6991, 6992, 6993, 6994, 6996, 6997, 6998,
				7001, 7002, 7003, 7004, 7005, 7007, 7008, 7009, 7010, 7011,
				7012, 7013, 7014, 7015, 7017, 7018, 7020, 7021, 7022, 7025,
				7026, 7028 };

		try {
			String[] arg0 = args[0].split(":");
			String rsAddress = arg0[0];
			int rsPort = Integer.parseInt(arg0[1].trim());
			if (args.length > 1 && args[1].equals("peer0")) {
				PeerClient peer0 = new PeerClient(rsAddress, rsPort, 65450,
						"./rfc_0/");
				peer0.runCommand(ProtocolClientImpl.REGISTER);
				peer0.runCommand(ProtocolClientImpl.PQUERY);
				peer0.runCommand(ProtocolClientImpl.RFCQUERY);

			} else {

				// Create Peer
				PeerClient peer1 = new PeerClient(rsAddress, rsPort, 65451,
						"./rfc_1/");
				PeerClient peer2 = new PeerClient(rsAddress, rsPort, 65452,
						"./rfc_2/");
				PeerClient peer3 = new PeerClient(rsAddress, rsPort, 65453,
						"./rfc_3/");
				PeerClient peer4 = new PeerClient(rsAddress, rsPort, 65454,
						"./rfc_4/");
				PeerClient peer5 = new PeerClient(rsAddress, rsPort, 65455,
						"./rfc_5/");

				// Register Peer
				peer1.runCommand(ProtocolClientImpl.REGISTER);
				peer2.runCommand(ProtocolClientImpl.REGISTER);
				peer3.runCommand(ProtocolClientImpl.REGISTER);
				peer4.runCommand(ProtocolClientImpl.REGISTER);
				peer5.runCommand(ProtocolClientImpl.REGISTER);

				// Update Peer List
				peer1.runCommand(ProtocolClientImpl.PQUERY);
				peer2.runCommand(ProtocolClientImpl.PQUERY);
				peer3.runCommand(ProtocolClientImpl.PQUERY);
				peer4.runCommand(ProtocolClientImpl.PQUERY);
				peer5.runCommand(ProtocolClientImpl.PQUERY);

				// Update RFC Index
				peer1.runCommand(ProtocolClientImpl.RFCQUERY);
				peer2.runCommand(ProtocolClientImpl.RFCQUERY);
				peer3.runCommand(ProtocolClientImpl.RFCQUERY);
				peer4.runCommand(ProtocolClientImpl.RFCQUERY);
				peer5.runCommand(ProtocolClientImpl.RFCQUERY);

				// Create Peer Thread
				Task1Thread peer1Thread = new Task1Thread("Peer1", peer1,
						rfcNumbers);
				Task1Thread peer2Thread = new Task1Thread("Peer2", peer2,
						rfcNumbers);
				Task1Thread peer3Thread = new Task1Thread("Peer3", peer3,
						rfcNumbers);
				Task1Thread peer4Thread = new Task1Thread("Peer4", peer4,
						rfcNumbers);
				Task1Thread peer5Thread = new Task1Thread("Peer5", peer5,
						rfcNumbers);

				// Run Threads
				peer1Thread.start();
				peer2Thread.start();
				peer3Thread.start();
				peer4Thread.start();
				peer5Thread.start();

			}
		} catch (Exception e) {
			Log.err("Usage:\n	RunTask1 <RS IP>:<RS Port> [Peer0]");
			Log.err("Example:\n	RunTask1 10.8.11.52:65423 peer0");
			Log.err("        	RunTask1 10.8.11.52:65423");
			System.exit(1);
		}
	}
}
